package com.example.statistics.common

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.example.statistics.R
import com.example.statistics.bean.MultiSelectorBean
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.glideCircle
import org.jetbrains.anko.toast
import java.lang.reflect.Type

class MultiSelectListActivity : BaseLoadingListActivity<MultiSelectorBean<*>>() {

    private var chooseResult: Boolean = true

    private var selectorKeyList: ArrayList<Int>? = arrayListOf()
    override val url: String
        get() = intent.getStringExtra(Constants.PATH).orEmpty()
    override val title: String
        get() = intent.getStringExtra(Constants.TITLE) ?: "多选"
    override val itemLayoutRes: Int
        get() = R.layout.list_item_selector
    override val inputSearch: Boolean
        get() = true
    override val inputSearchFunc: (MultiSelectorBean<*>, String) -> Boolean = { bean, str ->
        bean.name().contains(str)
    }

    override val httpParams: MutableList<Pair<String, String>>
        get() {
            val params = intent.getSerializableExtra(Constants.PARAMS) as? List<Pair<String, String>>?
            return params.orEmpty().toMutableList()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        selectorKeyList = intent.getIntegerArrayListExtra(Constants.DATA) as? ArrayList<Int>
        chooseResult = intent.getBooleanExtra("chooseResult", true)
        topBarRightText("确定") {
            val selectorList = dataList.filter { it.isSelector }
            if (chooseResult && selectorList.isEmpty()) {
                toast("请选择${title}")
                return@topBarRightText
            }
            activityFinish(Constants.DATA to selectorList)
        }
    }


    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: MultiSelectorBean<*>, position: Int) {
        injector.text(R.id.tv_single_title, bean.name())
                .with<ImageView>(R.id.iv_single_selector) {
                    it.visibility = View.VISIBLE
                    it.isSelected = bean.isSelector
                }
                .with<ImageView>(R.id.iv_avatar) {
                    it.visibility = if (bean.avatar().isNullOrEmpty()) View.GONE else View.VISIBLE
                    it.glideCircle(bean.avatar().orEmpty())
                }
                .clicked {
                    bean.isSelector = !bean.isSelector
                    adapter.notifyItemChanged(position)
                }
    }

    override fun formatList(dataList: MutableList<MultiSelectorBean<*>>): MutableList<MultiSelectorBean<*>> {
        dataList.forEach {
            if (selectorKeyList?.contains(it.key()) == true) it.isSelector = true
        }
        return dataList
    }

    override fun genericType(): Type {
        return intent.getSerializableExtra(Constants.TYPE) as Type
    }
}